/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// DataReal.cpp: implementation of the CDataReal class.
//
//////////////////////////////////////////////////////////////////////

#include "ssi_datareal.h"
#include "ssi_dataint.h"
#include "ssi_datareal.h"
#include <ctype.h>
#include <math.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SSI_SCRIPTDLL_API SSI_INT64 CDataReal::sType = 0;


CDataReal::CDataReal()
{
  Data = 0.0;
	if(!sType){
		sType = 1;
		sType = Factory_RegisterData((CData*)new CDataReal());
	}

  Type = sType;
	DataName = "real";
}

CDataReal::~CDataReal()
{

}

// MUST BE REDONE
SSI_BOOL CDataReal::CopyData(CData *Other)
{
  if((Type != Other->Type) && (Other->Type != CDataInt::GetRegisteredType())) return 0;
  if(Other == SSI_NULL) return 0;
  if(Other->Type == CDataInt::GetRegisteredType())
  {
    CDataInt* Temp = (CDataInt*)Other;
    Data = (REAL)Temp->Data;
  }
  else
  {
    CDataReal* Temp = (CDataReal*)Other;
    Data = Temp->Data;
  }
  
  return 1;
}

SSI_BOOL CDataReal::DataFromString(const String& sData)
{
  SSI_INT64 NumFields = 0;
	SSI_INT64 ctr = 0;
	SSI_CHAR c;
  if(sData == "")
  {
    Data = 0.0;
    return 0;
  }

  //Want it to accept an integer...
	//if(sData.Find(SSI_CHARS_DECIMAL) < 0) return 0;

	for(ctr = 0; ctr < sData.GetLength(); ctr++){
		c = sData[ctr];
		if(!isdigit(c)){
			if((c != '-') && (c != '+')	&& (c != '.') && (c != 'e') && (c != 'E')){
				return 0;
			}
		}
	}
	if((NumFields = sscanf(sData.Str(), "%lf", &Data)) == 1) return 1;
  //int i;
  //if((NumFields = sscanf(sData.Str(), "%d", &i)) == 1){
  //  Data = i;
  //  return 1;
  //}
  return 0;
}

String CDataReal::GetString()
{
  String Ret;
  Ret.Format("%14.14f", Data);
  return Ret;
}

SSI_INT64 CDataReal::SizeOf()
{
  return sizeof(Data);
}

SSI_BOOL CDataReal::boolean()
{
  if(Data) return 1;
  return 0;
}

SSI_INT64 CDataReal::GetRegisteredType(void)
{
	if(!sType){
		CDataReal aData;
	}
	return sType;
}
CData* CDataReal::operator_plus(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataInt::GetRegisteredType()){
    Data = (Data + (REAL)((CDataInt*)InData)->Data);
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data + ((CDataReal*)InData)->Data;
  }

	//if(InData->GetType() != Type) return SSI_NULL;
	//Data+=((CDataReal*)InData)->Data;
	return (CData*)this;
}

CData* CDataReal::operator_minus(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataInt::GetRegisteredType()){
    Data = (Data - (REAL)((CDataInt*)InData)->Data);
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data - ((CDataReal*)InData)->Data;
  }

	//if(InData->GetType() != Type) return SSI_NULL;
	//Data =Data - ((CDataReal*)InData)->Data;
	return (CData*)this;
}

CData* CDataReal::operator_multiply(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataInt::GetRegisteredType()){
    Data = (Data * (REAL)((CDataInt*)InData)->Data);
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data * ((CDataReal*)InData)->Data;
  }

	//if(InData->GetType() != Type) return SSI_NULL;
	//Data = Data * ((CDataReal*)InData)->Data;
	return (CData*)this;
}

CData* CDataReal::operator_divide(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataInt::GetRegisteredType()){
    Data = (Data / (REAL)((CDataInt*)InData)->Data);
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = Data / ((CDataReal*)InData)->Data;
  }

	//if(InData->GetType() != Type){
	//	if(InData->GetType() != CDataInt::GetRegisteredType()){
	//		return SSI_NULL;
	//	}
	//	SSI_INT64 d = ((CDataInt*)InData)->Data;
	//	Data = Data / (REAL)d;
	//}
	//Data = Data / ((CDataReal*)InData)->Data;
	return (CData*)this;
}

CData* CDataReal::operator_power(CData* InData, OpInfoT* Info)
{
  if(InData->GetType() == CDataInt::GetRegisteredType()){
    Data = pow(Data, (REAL)((CDataInt*)InData)->Data);
  }else	if(InData->GetType() != Type){
    return SSI_NULL;
  }else{
    Data = pow(Data, ((CDataReal*)InData)->Data);
  }

  return (CData*)this;
}

CData* CDataReal::operator_equal(CData* InData, OpInfoT* Info)
{
	if(InData->GetType() != Type){
		if(InData->GetType() != CDataInt::GetRegisteredType()){
			return SSI_NULL;
		}
		SSI_INT64 d = ((CDataInt*)InData)->Data;
		Data = (REAL)d;
	}else{
		Data = ((CDataReal*)InData)->Data;
	}	
	return (CData*)this;
}

CStatus CDataReal::negate(void)
{
	Data = -1 * Data;
	return STAT_OK;
}

CStatus CDataReal::operator_compare(CData* InData, SSI_INT64& Result)
{

  if(InData->GetType() != Type && InData->GetType() != CDataInt::GetRegisteredType()){
    return CData::operator_compare(InData, Result);
  }

  if(InData->GetType() == Type){
	CDataReal* In = (CDataReal*)InData;
	if(Data < In->Data){
		Result = -1;
	}else if(Data > In->Data){
		Result = 1;
	}else{
		Result = 0;
	}
  }else if(InData->GetType() == CDataInt::GetRegisteredType()){
	  CDataInt* In = (CDataInt*)InData;
	  if(Data < In->Data){
		  Result = -1;
	  }else if(Data > In->Data){
		  Result = 1;
	  }else{
		  Result = 0;
	  }
  }

	return STAT_OK;
}

CStatus CDataReal::operator_increment()
{
	Data++;
	return STAT_OK;
}

CStatus CDataReal::operator_decrement()
{
	Data--;
	return STAT_OK;
}

SSI_INT64 CDataReal::Register(SSI_INT64 NewType){
	sType = NewType;
	return 1;
}

void CDataReal::UnRegister(){
	sType = 0;
}



